﻿#include "stdafx.h"
#include "createDeviceApp.h"
#include "Src/FUI_InputEvent.h"
#include "Src/FUI_FuncInterface.h"

bool createDeviceApp::Create()
{
#ifdef _UI
	FUI_INIT it;
	it.hwnd  = fge->GetHwnd();
	it.wnd_w = fge->GetWidthF();
	it.wnd_h = fge->GetHeightF();
	it.base_dir = FAppGetAssetsRootA();
	it.userPTR  = (DWORD_PTR)fge;
	FUI_InitFuncInterface(it);
	uiSys.Initialize(_FTA("FrameMain.fud"), &it);
	myWnd.LoadUI(&uiSys);
	
	insA.id = 1;
	insB.id = 2;
	insA.ins = FFString(INS_A);
	insB.ins = FFString(INS_B);
	
	
#else
	sprx = 300, spry = 280;

	fge->GetFont()->GetFontData(eCD_DefFontID)->nBoldLevel = 1;

			        // 创建新字体，默认字体同时也存在，这样引擎中就有了2套字体
	fnt_id = fge->GetFont()->Create(FFAppGetAssets(_FFT("font/stliti.ttf")), 30, false, true);

	FFString filename(FFAppGetAssets(_FFT("example/example.png")));
#if 1 // 方式一：通过文件内存加载（FFileRead接口自动适应各操作系统平台文件系统，安卓路径可以是APK包中相对路径，也可以是SD中全路径）
	FV_BYTE buf;
	FFFileRead(filename, &buf);
	tex_id = fge->GetTexture()->Create(&buf[0], buf.size());
#else // 方式二：直接从文件系统加载
	tex_id = fge->GetTexture()->Create(filename);
#endif
#endif


#ifdef _CTP_API
	FAutoDll infoDll;
	CTP_CREATE_0 ctp_create;
	FFString os_file = _FFT(CTP_QUOTE_SO);
	infoDll.Load(os_file);
	infoDll.GetFunc(ctp_create, _CreateFtdcMdApi_4);
	if (!ctp_create)
	{
		FOutDebugStringA(FAssertFmtA(_FTA("YCTP>> %s is null)\n"), "ctp_create"));
		return true;
	}

	FFString conPath = FFAppGetTemp("");
	FOutDebugStringA(FAssertFmtA(_FTA("[err]YCTP>> %s >>> load Dialog.con)\n"), conPath.c_str()));
	char * frontAddr = "tcp://180.168.146.187:10010";
	//Create
	pMDApi = (*ctp_create)(conPath.c_str(), false, false);
	if (!pMDApi)
	{		    
		FOutDebugStringA(FAssertFmtA(_FTA("[err]YCTP>> %s ,err)\n"), "ctp_create"));
		return true;
	}
	else
	{		    
		FOutDebugStringA(FAssertFmtA(_FTA("[detail]YCTP>> %s ,ok)\n"), "ctp_create"));
	}
	//Connect
	protocol.SetEventHandle(this);
	pMDApi->RegisterSpi(&protocol);
	pMDApi->RegisterFront(frontAddr);
	pMDApi->Init();
#endif

	return true;
}
void createDeviceApp::Destroy()
{
    // 消毁我们创建的贴图
	fge->GetTexture()->DestroyEx(tex_id);
	// 消毁我们创建的字体，默认字体由引擎自动消毁
	fge->GetFont()->Destroy(fnt_id);
}
void createDeviceApp::Render()
{
    // 对渲染逻辑进行性能统计，性能日志存在log\[date-time]\profile - [app_name].log中
	FPF_FuncAuto;
	    
#ifdef _UI
	uiSys.Render();
	uiSys.RenderTips();
#else
	 

		 			         // 带有0.5缩放的渲染，当然也可以用 DrawSpriteEx，所以下面两种渲染模式是一样的效果
#if 1
	fge->GetRender()->DrawSprite(tex_id, 0, fge->GetHeightF() - 256 / 2, 256 / 2, 256 / 2, 0, 0, 256, 256);
#else
	fge->GetRender()->DrawSpriteEx(tex_id, 0, fge->GetHeightF() - 256, 0, 0, 256, 256, 0, 0, 0, 0.5f);
#endif

				            // 带截取、旋转、中心点的渲染（每秒90度）
	fge->GetRender()->DrawSpriteEx(tex_id, sprx, spry, 0, 0, 64, 64, 32, 32, (float)fge->GetTime() * F_PI2, 1, 0, eRM_None, 0xffffffff, eRF_Default2D | eRF_TextureFilter);

	float fnt_step = (float)fge->GetFont()->GetFontData(eCD_DefFontID)->nFontHeight;
	int fnt_line = 0;
	// 格式化字符串，FAssertFmt在DEBUG和RELEASE中都可以用，且多线程安全。
	// 为了适应所有平台，在格式化字符串时用多字节，WINDOWS平台为ANSI，其它平台为UTF-8
	// 安卓不支持宽字节格式化
	const FFCHAR* fmt_str = FFAssertFmt(_FFT("FPS：%.02f， SIZE：%d x %d， ♡尐龍孖﹍，Hello，Fuel4D！\nSYS.TICK: %u\nFGE.TIME: %f"),
		fge->GetFPS(),
		fge->GetWidthI(),
		fge->GetHeightI(),
		FGetTime(),
		(float)fge->GetTime());

							            // 打印普通信息
	fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), fmt_str, eCD_DefFontID, eCD_DefFntColor, eFE_Shadow);
	fnt_line += 2;

				            // 打印当前渲染器使用的渲染管线，此设置可以在FNewApplication接口中初始化参数中设置
	if (fge->HasEnvType(eET_2D_Fixed))
		fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), _FFT("2D渲染使用：固定管线"), eCD_DefFontID, eCD_DefFntColor, eFE_Shadow);
	if (fge->HasEnvType(eET_2D_Shader))
		fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), _FFT("2D渲染使用：可编程管线"), eCD_DefFontID, eCD_DefFntColor, eFE_Shadow);

							            // 打印当前图形渲染设备类型
	switch (fge->GetGraph()->nType)
	{
	case eDT_D3D9 : fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), _FFT("渲染设备：D3D9"), eCD_DefFontID, eCD_DefFntColor, eFE_Shadow); break;
	case eDT_D3D11: fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), _FFT("渲染设备：D3D11"), eCD_DefFontID, eCD_DefFntColor, eFE_Shadow); break;
	case eDT_GLES2: fge->GetFont()->DrawTextEx(10, 10 + fnt_step * (fnt_line++), _FFT("渲染设备：GLES2"), eCD_DefFontID, eCD_DefFntColor, eFE_Shadow); break;
	}

				            // 用指定字体打印文字，带颜色渐变等效果
	fge->GetFont()->DrawGradation(11,
		10 + fnt_step * fnt_line,
		_FFT("class IFEngine：是整个引擎接口\n")\
		_FFT("class IFSystem：是硬件查询系统\n")\
		_FFT("class IFApplication：是应用程序对象基类"),
		fnt_id,
		0xffff0000,
		0xff00ff00,
		0xff0000ff,
		0xffffffff,
		eFGM_Char);
	fge->GetFont()->DrawGradation(10,
		10 + fnt_step * fnt_line,
		_FFT("class IFEngine：是整个引擎接口\n")\
		_FFT("class IFSystem：是硬件查询系统\n")\
		_FFT("class IFApplication：是应用程序对象基类"),
		fnt_id,
		0xffff0000,
		0xff00ff00,
		0xff0000ff,
		0xffffffff,
		eFGM_Whole);

							            // 使用指定模式打印到指定位置，本例为右下角+描边
							            // 整个渲染区，并右下角收缩10象素
	FRect wnd_rc(0, 0, fge->GetWidthF() - 10.0f, fge->GetHeightF() - 10.0f);
	fge->GetFont()->DrawText(&wnd_rc, _FFT("燃料站(R)"), eCD_DefFontID, eCD_DefFntColor, eFA_RIGHT | eFA_BOTTOM, eFE_Fringe);
	       
#endif

#ifdef _CTP_API
	    
	FRect wnd_rc_A(0, 0, fge->GetWidthF() - 10.0f, fge->GetHeightF() - 50.0f);
	fge->GetFont()->DrawText(&wnd_rc_A, szInsA, eCD_DefFontID, eCD_DefFntColor, eFA_RIGHT | eFA_BOTTOM, eFE_Fringe);
	    
	FRect wnd_rc_B(0, 0, fge->GetWidthF() - 10.0f, fge->GetHeightF() - 100.0f);
	fge->GetFont()->DrawText(&wnd_rc_B, szInsB, eCD_DefFontID, eCD_DefFntColor, eFA_RIGHT | eFA_BOTTOM, eFE_Fringe);
	
	    
#endif // _CTP_API

}
bool createDeviceApp::OnInput(FINPUT_PACKAGE& ipk)
{
	static bool is_down = false;
	switch (ipk.nType)
	{
	case eIT_KEYUP:
		if (ipk.p1 == VK_ESCAPE)
			fge->Quit();
		else if (ipk.p1 == 'A')
			fge->GetFont()->__OutputFntTex(eCD_DefFontID, _FTA("d:\\aaa.png"));
		break;
	case eIT_LBUTTON_DOWN:
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		is_down = true;
		break;
	case eIT_MOUSEMOVE:
		if (is_down)
		{
			sprx = ipk.p2.v.v2.x;
			spry = ipk.p2.v.v2.y;
		}
		break;
	case eIT_LBUTTON_UP:
		is_down = false;
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		break;
	}
	return false;
}
	
void createDeviceApp::Update()
{
	uiSys.Update();
}


void createDeviceApp::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)  
{
	
	
	if (strcmp(pDepthMarketData->InstrumentID, INS_A) == 0)
	{
		szInsA = FAssertFmtA(_FTA("YCTP>> %s >>> %.1f\n"), INS_A, pDepthMarketData->LastPrice);
		insA.price = pDepthMarketData->LastPrice;
		myWnd.MakeIns(insA, true);
	}
	else if (strcmp(pDepthMarketData->InstrumentID, INS_B) == 0)
	{
		szInsB = FAssertFmtA(_FTA("YCTP>> %s >>> %.1f\n"), INS_B, pDepthMarketData->LastPrice);	
		insB.price = pDepthMarketData->LastPrice;
		myWnd.MakeIns(insB, true);
		
	}
	
}